using UnityEngine;
namespace NuoYan.Extension
{
    public static class RectTransformExtension
    {
        /// <summary>
        /// 判断一个点是否在方形Rect内
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static bool Contains(this RectTransform rect, Vector2 point)
        {
            return rect.GetBounds().Contains(point);
        }
        /// <summary>
        /// 判断两个方形Rect是否相交
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static bool Intersects(this RectTransform rect1, RectTransform rect2)  // Unity自身提供判断两个方形Rect是否相交的方法  Rect中的 Overlaps方法
        {
            Bounds b1 = rect1.GetBounds();
            Bounds b2 = rect2.GetBounds();
            return b1.Intersects(b2);
        }
        /// <summary>
        /// 获取两个方形Rect相交的百分比，结果为相交体积与第一个包围盒体积的比例
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static float IntersectPercentage(this RectTransform rect1, RectTransform rect2)
        {
            Bounds b1 = rect1.GetBounds();
            Bounds b2 = rect2.GetBounds();
            return b1.IntersectPercentage(b2);

        }
        /// <summary>
        /// 获取方形Rect的包围盒
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static Bounds GetBounds(this RectTransform rect)
        {
            Vector3[] corners = new Vector3[4];
            rect.GetWorldCorners(corners);
            Vector2 min = corners[0];
            Vector2 max = corners[2];
            return new Bounds((min + max) / 2, max - min);
        }
        /// <summary>
        /// 计算第一个包围盒与第二个包围盒相交的百分比，结果为相交体积与第一个包围盒体积的比例
        /// </summary>
        /// <param name="bounds1"></param>
        /// <param name="bounds2"></param>
        /// <returns></returns>
        public static float IntersectPercentage(this Bounds bounds1, Bounds bounds2)
        {
            // 计算相交区域
            Bounds intersection = new Bounds();
            if (!bounds1.Intersects(bounds2))
                return 0f;

            intersection.SetMinMax(
                Vector3.Max(bounds1.min, bounds2.min),
                Vector3.Min(bounds1.max, bounds2.max)
            );

            // 计算相交体积与第一个包围盒体积的比例
            float intersectionVolume = intersection.size.x * intersection.size.y * intersection.size.z;
            float bounds1Volume = bounds1.size.x * bounds1.size.y * bounds1.size.z;

            return intersectionVolume / bounds1Volume;
        }
        /// <summary>
        /// 获取Canvas的RectTransform
        /// </summary>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public static RectTransform CanvasRectTransform(this Canvas canvas)
        {
            return canvas.GetComponent<RectTransform>();
        }
    }
}

